/**
 * 表格扩展按钮click事件
 */
import {ajax} from 'nc-lightapp-front';
import afterEvent from './afterEvent';
import {headSumHandle} from './commonEvent';
const tableInnerBtnAction = window.presetVar.tableInnerBtnAction;
const body1 = window.presetVar.body.body1;
const head1 = window.presetVar.head.head1;
const statusVar = window.presetVar.status;

function tableExtendButtonClick(bodyCode) {
    return {
        [bodyCode + tableInnerBtnAction.Edit] : {
            afterClick: (record, index) => {
                // 切换展开/收起按钮
                if (record) {
                    if (record.expandRowStatus) { // 收起
                        let elem = document.querySelector(`.u-table-fixed-right tr[data-row-key='${record.rowid}'] a.row-btn-${body1}_Edit`);
                        // elem.innerHTML = '收起';
                    } else { // 展开
                        let elem = document.querySelector(`.u-table-fixed-right tr[data-row-key='${record.rowid}'] a.row-btn-${body1}_Edit`);
                        // elem.innerHTML = '展开';
                    }
                }
            }
        },
        [bodyCode +  tableInnerBtnAction.Delete ] : {
            afterClick: (record, index) => {
                headSumHandle.call(this,this.props);
            }
        },
        [bodyCode +  tableInnerBtnAction.Copy]:{
            afterClick: (record, index) => {
                 let changedrows =  this.props.cardTable.getRowsByIndexs(bodyCode, 0);
                 let value = this.props.cardTable.getValByKeyAndIndex(bodyCode, 0,'orig_amount');
                 let prop = this.props;
                 afterEvent.call(this,prop,bodyCode,'orig_amount',value , changedrows);
            }
        },
        [bodyCode +  tableInnerBtnAction.Insert]:{
            afterClick: (record, index) => {
                let rowIndex = this.props.cardTable.getVisibleRows(bodyCode).length - 1;
                // 增行时，本币汇率、汇率类型、汇率来源日期，需根据表头来设置表体其编辑性
                let isEditKeys = ['org_currinfo','zzhllx','zzhllyrq'] //表体需要控制的编辑性key
                let editkeys = []
                let unedikeys = []
                isEditKeys.forEach((item) => {
                    this.props.form.getFormItemsDisabled('head', item) ? unedikeys.push(item) : editkeys.push(item)
                })
                this.props.cardTable.setEditableByIndex(bodyCode, rowIndex, editkeys, true)
                this.props.cardTable.setEditableByIndex(bodyCode, rowIndex, unedikeys, false)
            }
        },
    }
}

export default tableExtendButtonClick;
